/**
 * 
 */
package org.softTank.BC.flex;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
import java.util.Properties;

import org.apache.log4j.Logger;
import org.softTank.BC.manager.OperationManager;
import org.softTank.BcpModel.BcpPartnerLink;
import org.softTank.BcpModel.BcpProcess;
import org.softTank.BcpModel.BcpVariable;
import org.softTank.BpelTranslator.Translator;
import org.softTank.BpelTranslator.Impl.TranslatorFactory;

import MyCodes.BPEL;

/**
 * @author Yuzone
 *
 */
public class BpelTools {

	protected Logger logger = Logger.getLogger(this.getClass());
	static String DIR = "";

	public BpelTools() {
		Properties p = new Properties();
		try {
			p.load(this.getClass().getClassLoader().getResourceAsStream(
					"application.properties"));
			DIR = p.get("wsdl_address").toString();
		} catch (IOException e) {
			logger
					.warn("Cannot find .properties file!(use default address now) "
							+ e.getMessage());
		}
	}
	public String translate(BcpProcess bp) {
		Translator btb = TranslatorFactory.createTranslator();
		String code;
		try {
			code = btb.bcp2Bpel(bp);
		} catch (Exception e) {
			code = "error occured!";
			logger.debug(e.getMessage());
		}
		try {
			BufferedWriter bw = new BufferedWriter(new FileWriter(
DIR
					+ "MyBpel.bpel"));
			bw.write(code);
			bw.flush();
			bw.close();
		} catch (IOException e1) {
			logger.debug(e1.getMessage());
		}

		return code;
	}

	public void send2Engine() {
		BPEL bpel = new BPEL(DIR, "MyBpel.bpel");
		try {
			bpel.doAll();
			Thread.sleep(3000l);
			bpel.send();
		} catch (Exception e) {
			logger.debug("There is something wrong in BPEL class: "
					+ e.getMessage());
		}
	}

	public BcpPartnerLink getPartnerLink(String id, String operation, int type) {
		return new OperationManager().getPartnerLink(id, operation, type);
	}

	public List<BcpVariable> getVariable(String serviceID, String operation) {
		return new OperationManager().getVariable(serviceID, operation);
	}

}
